package tq.com.tqstudent.ui.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;

import com.lib.common.base.BaseFragment;
import com.lzy.okgo.OkGo;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.RefreshState;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.ArrayList;

import butterknife.BindView;
import tq.com.tqstudent.App;
import tq.com.tqstudent.Constants;
import tq.com.tqstudent.R;
import tq.com.tqstudent.adapter.RecodeAdapter;
import tq.com.tqstudent.bean.BaseBean;
import tq.com.tqstudent.bean.RecodeBean;
import tq.com.tqstudent.bean.UserInfo;
import tq.com.tqstudent.net.ok.DialogCallback;
import tq.com.tqstudent.utils.VarietyUtil;

/**
 * @Author rames
 * @date 2019/2/27 11:17
 */
public class RecodeFragment extends BaseFragment {
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.empty_layout)
    LinearLayout emptyLayout;
    private RecodeAdapter adapter;
    private boolean isLoaded = false;
    private int pageNO = 1;

    @Override
    protected int setView() {
        return R.layout.fragment_recode;
    }

    @Override
    protected void init(View view) {
        if (isVisible() && isLoaded) {
            return;
        }
        isLoaded = true;

        refreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                pageNO++;
                getData();
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                pageNO = 1;
                getData();
            }
        });

        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        ArrayList<RecodeBean> list = new ArrayList<>();
        adapter = new RecodeAdapter(getActivity(), list);
        recyclerView.setAdapter(adapter);

        changeStatus();
    }

    private void changeStatus(){
        if(!UserInfo.isLogin()){
            emptyLayout.setVisibility(View.VISIBLE);
            recyclerView.setVisibility(View.GONE);
            VarietyUtil.setRefreshEnable(refreshLayout, false);
        }
    }

    private ArrayList<RecodeBean> buildData(ArrayList<RecodeBean> data){
        ArrayList<RecodeBean> list = new ArrayList<>();
        ArrayList<String> titles = getSameTitle(data);
        String mTitle = "";

        for (int i = 0; i < titles.size(); i++) {
            mTitle = titles.get(i);
            list.add(RecodeBean.initTitle(mTitle));

            if(getSameWK(data, mTitle).size() > 0){
                list.addAll(getSameWK(data, mTitle));
            }

            if(getSameJP(data, mTitle).size() > 0){
                list.addAll(getSameJP(data, mTitle));
            }
        }

        return list;
    }

    private ArrayList<String> getSameTitle(ArrayList<RecodeBean> data){
        ArrayList<String> list = new ArrayList<>();

        for (int i = 0; i < data.size(); i++) {
            list.add(data.get(i).meterial_title);
        }

        for (int i = 0; i < list.size(); i++) {
            for (int j = list.size()-1; j > i; j--) {
                if(list.get(j).equals(list.get(i))) {
                    list.remove(j);
                }
            }
        }

        return list;
    }

    private ArrayList<RecodeBean> getSameWK(ArrayList<RecodeBean> data, String title){
        ArrayList<RecodeBean> list = new ArrayList<>();
        RecodeBean bean = null;
        String cTime = "";
        int sTime = 0;

        for (int i = 0; i < data.size(); i++) {
            bean = data.get(i);
            if(bean.meterial_cate == 15 && bean.meterial_title.equals(title)){
                if(TextUtils.isEmpty(cTime)) {
                    cTime = bean.create_time.split(" ")[0];
                }
                sTime += Integer.parseInt(bean.watch_sec);
                list.add(RecodeBean.initContent(bean.material_id, bean.title, bean.content));
            }
        }

        if(list.size() > 0) {
            list.add(RecodeBean.initTime(cTime, sTime + ""));
        }

        return list;
    }

    private ArrayList<RecodeBean> getSameJP(ArrayList<RecodeBean> data, String title){
        ArrayList<RecodeBean> list = new ArrayList<>();
        RecodeBean bean = null;

        for (int i = 0; i < data.size(); i++) {
            bean = data.get(i);
            if(bean.meterial_cate == 16 && bean.meterial_title.equals(title)){
                list.add(RecodeBean.initImage(bean.material_id, bean.content, bean.content_cover, bean.view_cnt));
            }
        }

        return list;
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        changeStatus();
        if(null == adapter || adapter.getDatas().size() < 1) {
            if(UserInfo.isLogin()) {
                refreshLayout.autoRefresh();
            }
        }
    }

    private void getData() {
        String uri = "api/clz/user/history/"+ Constants.PAGE_SIZE+"/"+pageNO;
        OkGo.<BaseBean<ArrayList<RecodeBean>>>get(App.getHttpUrl(uri))
                .tag(this)
                .execute(new DialogCallback<ArrayList<RecodeBean>>(getActivity()) {
                    @Override
                    public void handleSuccess(ArrayList<RecodeBean> data) {
                        // 这是加载更多
                        if(refreshLayout.getState() == RefreshState.Loading){
                            adapter.addData(buildData(data));
                            refreshLayout.finishLoadMore();
                            if(data.size() < Constants.PAGE_SIZE){
                                refreshLayout.setEnableLoadMore(false);
                            }

                            // 这是下拉刷新
                        } else {
                            adapter.getDatas().clear();
                            adapter.setData(buildData(data));
                            refreshLayout.setEnableLoadMore(true);
                            refreshLayout.finishRefresh();
                        }

                        emptyLayout.setVisibility(recyclerView.getAdapter().getItemCount() > 0 ? View.GONE : View.VISIBLE);
                    }

                    @Override
                    public void handleFail(BaseBean body) {
                        super.handleFail(body);
                        refreshLayout.finishLoadMore();
                        refreshLayout.finishRefresh();
                    }
                });
    }
}
